//
//  MainViewController.swift
//  我的微博
//
//  Created by 杨庆 on 15/10/9.
//  Copyright © 2015年 双元班. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addAllChildViewController()
        //此时的 tabbar 的button 没有被创建;
        print(tabBar.subviews)
    }
/// 自定义一个新的方法,来实现button ;
    func clickComposedButton(){
        //打印的 当前的类的名称;
    print(__FUNCTION__)
    }
/// 点击,和设置撰写的按钮
    private func setUpComposedButton(){
/// 打印并 调用 懒加载;
        print(composeButton)
        let w = tabBar.bounds.width /  CGFloat(viewControllers!.count)
        let rect = CGRectMake(0, 0,w ,tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2*w, 0)
    }
/// 在运行一个函数,view 的didappear ;
    override func viewDidAppear(animated: Bool) {
        //此时的 tabbar 的button 才进行的创建;
        setUpComposedButton()
    }
/// 添加所有的子控件;
    private func addAllChildViewController(){
        //只是添加了控件,并没有进行添加,懒加载的情况无处不在;
        addChildViewController(home____TableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(Message____TableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(Discover____TableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(profile___TableViewController(), title: "我", imageName: "tabbar_profile")
    }
//swift 中一旦定义的方法和属性都是 全局型,为了安全的只在本文件中进行的访问的属性;我们要求应用一个 private 的关键字来进行的访问;
    private func addChildViewController(vc : UIViewController,title : NSString,imageName : NSString) {
        let vc = vc
        vc.title=title as String
        vc.tabBarItem.image=UIImage(named: imageName as String)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
// MARK:-懒加载的控件来进行的,遮罩视图
    lazy private var composeButton : UIButton = {
    //自定义类型的 按钮;
        let button=UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        self.tabBar.addSubview(button)
        button .addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
}
